package com.hck.infrastructure.persistent.repository;

import com.hck.domain.consultation.model.entity.HealthConsultationEntity;
import com.hck.domain.consultation.repository.IHealthConsultationRepository;
import com.hck.infrastructure.persistent.dao.IHealthConsultationDao;
import com.hck.infrastructure.persistent.po.HealthConsultation;
import com.hck.infrastructure.redis.IRedisService;
import com.hck.types.common.Constants;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * HealthConsultationRepository class description here.
 *
 * @author 黄朝看
 * @date 2025/6/30
 */
@Repository
public class HealthConsultationRepository implements IHealthConsultationRepository {
    @Resource
    private IHealthConsultationDao healthConsultationDao;

    @Resource
    private IRedisService redisService;

    @Override
    public void save(HealthConsultationEntity consultationEntity) {
        HealthConsultation healthConsultation = new HealthConsultation();
        BeanUtils.copyProperties(consultationEntity, healthConsultation);
        healthConsultationDao.insert(healthConsultation);
        consultationEntity.setId(healthConsultation.getId());
    }

    @Override
    public HealthConsultationEntity findById(Long id) {
        //先从redis获取
        String cacheKey = Constants.CONSULTATION_KEY + id;
        HealthConsultationEntity healthConsultationEntity = redisService.getValue(cacheKey);

        //缓存里存在
        if(healthConsultationEntity != null)
            return healthConsultationEntity;

        //缓存不存在，从数据库获取
        HealthConsultation healthConsultation = healthConsultationDao.selectById(id);
        if (healthConsultation == null) return null;
        healthConsultationEntity = new HealthConsultationEntity();
        BeanUtils.copyProperties(healthConsultation, healthConsultationEntity);
        //保存到redis
        redisService.setValue(cacheKey,healthConsultationEntity);
        return healthConsultationEntity;
    }

    @Override
    public List<HealthConsultationEntity> findByUserId(Long userId) {
        //从redis获取
        String cacheKey = Constants.CONSULTATION_LIST_KEY + userId;
        List<HealthConsultationEntity> list = redisService.getValue(cacheKey);

        if(list != null )
            return list;

        list = healthConsultationDao.selectByUserId(userId).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());

        redisService.setValue(cacheKey,list);
        return list;
    }

    @Override
    public List<HealthConsultationEntity> findByDoctorId(Long doctorId) {
        //从redis获取
        String cacheKey = Constants.CONSULTATION_LIST_KEY + doctorId;
        List<HealthConsultationEntity> list = redisService.getValue(cacheKey);

        if(list != null )
            return list;

        list = healthConsultationDao.selectByUserId(doctorId).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());

        redisService.setValue(cacheKey,list);
        return list;
    }

    @Override
    public void updateStatus(Long id, String status, Date replyTime, Date closeTime) {
        healthConsultationDao.updateStatus(id, status, replyTime, closeTime);
    }
}
